var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectDescribe: "",
    issuccessMask: false
  },
  del(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    let pinlun = this.data.pinlun
    let imageList = pinlun.buildAtlas
    let t = this
    wx.showModal({
      content: '是否要删除该图片',
      confirmColor: "#D6000F",
      success(res) {
        if (res.confirm) {
          imageList.splice(index, 1)
          // let str = imageList.join(",")
          pinlun.buildAtlas = imageList
          t.setData({
            pinlun: pinlun
          })
        }

      }
    })
  },


  addpricture() {
    let t = this
    let pinlun = this.data.pinlun
    let buildAtlas = pinlun.buildAtlas
    let bignumber = 9 - buildAtlas.length
    wx.chooseMedia({
      count: bignumber,
      mediaType: 'image',
      maxDuration: 60,
      success(res) {
        let finishFilesList = []
        res.tempFiles.forEach((item, index) => {
          finishFilesList.push(item.tempFilePath)
        })
        buildAtlas.push(...finishFilesList)
        pinlun.buildAtlas = buildAtlas
        t.setData({
          pinlun: pinlun
        })
      }
    })
  },

  // 查看图片
  priview(e) {
    console.log(e)
    let current = e.currentTarget.dataset.current
    let urls = e.currentTarget.dataset.urls.map(item => this.data.Imageurl + item)
    wx.previewImage({
      urls: urls,
      current: current
    })

  },
  // // 项目详情接口
  // getOrderDetails() {
  //   let t = this
  //   wx.showLoading({
  //     title: '加载中。。。', //提示的内容
  //     mask: true //显示透明蒙层 防止触摸穿透
  //   })
  //   wx.request({
  //     url: app.globalData.Interfaceurl + 'getOrderDetails',
  //     method: "post",
  //     header: {
  //       "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
  //     },
  //     data: {
  //       userId: app.globalData.userId,
  //       // userId: 1569562832576524288,
  //       orderId: t.data.orderId,
  //     },
  //     success: res => {
  //       wx.hideLoading();
  //       res.data.buildList.forEach(item => {
  //         let buildAtlas = item.buildAtlas.split(",").map(item1 => t.data.Imageurl + item1)
  //         buildAtlas = buildAtlas.join(",")

  //         item.buildAtlas = buildAtlas

  //       });

  //       t.setData({
  //         buildList: res.data.buildList,

  //       })
  //       if (res.data.buildList.length == 0) {
  //         return
  //       } else {
  //         t.setData({
  //           projectDescribe: res.data.buildList[0].buildSuperviseExplain,

  //         })

  //       }
  //     },
  //     fail: err => {
  //       wx.showToast({
  //         title: '请求失败',
  //         duration: 2000, //持续的时间
  //         icon: 'none', //图标
  //         mask: true //显示透明蒙层 防止触摸穿透
  //       })
  //     }
  //   })
  // },


  // 上传附件
  upload(arr) {
    return new Promise((reslove, reject) => {
      if (arr.length == 0) {
        reslove([])
        return
      }
      wx.showLoading({
        title: '上传中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      let finishFilesList = []
      arr.forEach((item, index) => {
        wx.uploadFile({
          url: app.globalData.Interfaceurl + 'saveFileUpload',
          filePath: item,
          name: 'file',
          formData: {
            userId: app.globalData.userId,

          },
          header: {
            "content-type": "multipart/form-data"
          },
          success: function (res) {
            finishFilesList.push(JSON.parse(res.data).fileUrl)
            if (index >= arr.length - 1) {
              setTimeout(() => {
                wx.hideLoading();
                reslove(finishFilesList)
              }, 500)
            }
          },
          fail: err => {
            return err
          }
        })
      })
    })


  },

  // 编辑施工日志
  saveOrderBuild() {
    let t = this








    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'saveOrderBuild',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        atlas: t.data.pinlun.buildAtlas,
        orderId: t.data.orderId,
        token: t.data.token,
        buildId: t.data.pinlun.id,
        node: "施工中",
        buildExplain: t.data.projectDescribe,
        atlas: t.data.pinlun.buildAtlas
      },
      success: res => {
        wx.hideLoading();
        if (res.data.result != -1) {
          this.setData({
            issuccessMask: true,

          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token: t.token()
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },


  unplone() {
    if (this.data.projectDescribe == '') {
      wx.showToast({
        title: '请填写内容后再上传',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }



    let pinlun = this.data.pinlun
    let buildAtlas = pinlun.buildAtlas
    let ybuildAtlas = buildAtlas.filter(item => item.indexOf(this.data.Imageurl) != -1)
    let nbuildAtlas = buildAtlas.filter(item => item.indexOf(this.data.Imageurl) == -1)
    let t = this
    this.upload(nbuildAtlas)
      .then(res => {
        buildAtlas = [...res, ...ybuildAtlas]
        let buildAtlas = buildAtlas.map(item => item.replace(this.data.Imageurl, ""))
        pinlun.buildAtlas = buildAtlas.join(",")
        this.saveOrderBuild()
      })




  },
  HidesuccessMask() {

    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getOrderDetails()
    wx.navigateBack({
      delta: 1,
    })


  },
  goprojectDetails() {
    wx.navigateTo({
      url: '/pages/projectDetails/projectDetails?orderId=' + this.data.orderId,
    })
  },
  fakeCallback() {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let pinlun = JSON.parse(options.pinlun)
    pinlun.buildAtlas = pinlun.buildAtlas.split(",")
    pinlun.buildAtlas = pinlun.buildAtlas.map(item => app.globalData.Imageurl + item)
    this.setData({
      identity: app.globalData.identity,
      orderId: options.orderId,
      Imageurl: app.globalData.Imageurl,
      token: app.token(),
      pinlun: pinlun,
      projectDescribe: JSON.parse(options.pinlun).buildSuperviseExplain,

    })
    // let pinlun=this.data.pinlun
    //     let buildAtlas = pinlun.buildAtlas.split(",").map(item => this.data.Imageurl + item)
    //     buildAtlas = buildAtlas.join(",")
    // pinlun
    // this.setData({
    //   pinlun: pinlun
    // })


    //     let buildAtlas=item.buildAtlas.split(",").map(item1=>t.data.Imageurl+item1)
    // buildAtlas=  buildAtlas.join(",")

    // item.buildAtlas=buildAtlas
    // this.getOrderDetails()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})